import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

public class StreamAccess {
    FileSystem fs = null;

    @Before
    public void init() throws Exception {
        Configuration conf = new Configuration();
//        conf.set("fs.defaultFS", "hdfs://10.211.55.8:9000");
//        conf.set("dfs.replication", "3");
        fs = FileSystem.get(new URI("hdfs://10.211.55.8:9000"), conf, "root");
    }

    @Test
    public void testDownLoadFileToLocal() throws IOException {
        FSDataInputStream in = fs.open(new Path("/input/test.txt"));
        FileOutputStream out = new FileOutputStream(new File("src/output/streamoutput.txt"));
        IOUtils.copyBytes(in,out,4096);
    }

    @Test
    public void testRandomAccess() throws IOException {
        FSDataInputStream in = fs.open(new Path("/input/test.txt"));
        //流起始偏移量
        in.seek(22);
        FileOutputStream out = new FileOutputStream(new File("src/output/randomAccess.txt"));
        IOUtils.copyBytes(in, out, 219L, true);

    }


}
